package com.bootdo.fx.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.common.utils.*;
import com.bootdo.fx.domain.UserInfoDO;
import com.bootdo.fx.service.UserInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.fx.domain.UserAddressDO;
import com.bootdo.fx.service.UserAddressService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * 
 * @author 袁飞龙
 * @email 1992lcg@163.com
 * @date 2017-11-30 17:12:20
 */
 
@Controller
public class UserAddressController {
	@Autowired
	private UserAddressService userAddressService;

	@Autowired
	private IdWorker idWorker;

	@Autowired
	private UserInfoService userInfoService;
	
	@GetMapping("/fx/userAddress")
	@RequiresPermissions("fx:userAddress:userAddress")
	String UserAddress(){
	    return "fx/userAddress/userAddress";
	}
	
	@ResponseBody
	@GetMapping("/fx/userAddress/list")
	@RequiresPermissions("fx:userAddress:userAddress")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<UserAddressDO> userAddressList = userAddressService.list(query);
		int total = userAddressService.count(query);
		PageUtils pageUtils = new PageUtils(userAddressList, total);
		return pageUtils;
	}
	
	@GetMapping("/fx/userAddress/add")
	@RequiresPermissions("fx:userAddress:add")
	String add(){
	    return "fx/userAddress/add";
	}

	@GetMapping("/fx/userAddress/edit/{addrId}")
	@RequiresPermissions("fx:userAddress:edit")
	String edit(@PathVariable("addrId") Long addrId,Model model){
		UserAddressDO userAddress = userAddressService.get(addrId);
		model.addAttribute("userAddress", userAddress);
	    return "fx/userAddress/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/fx/userAddress/save")
	@RequiresPermissions("fx:userAddress:add")
	public R save( UserAddressDO userAddress){
		if(userAddressService.save(userAddress)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/fx/userAddress/update")
	@RequiresPermissions("fx:userAddress:edit")
	public R update( UserAddressDO userAddress){
		userAddressService.update(userAddress);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/fx/userAddress/remove")
	@ResponseBody
	@RequiresPermissions("fx:userAddress:remove")
	public R remove( Long addrId){
		if(userAddressService.remove(addrId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/fx/userAddress/batchRemove")
	@ResponseBody
	@RequiresPermissions("fx:userAddress:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] addrIds){
		userAddressService.batchRemove(addrIds);
		return R.ok();
	}



	/*
    * 新增用户收货地址
    */
	@RequestMapping("/wx/userAddress/insertAddress")
	@ResponseBody
	public JsonResult insertAddress(@RequestParam Map<String,Object> params, HttpServletRequest request, HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		UserAddressDO ua = new UserAddressDO();
		ua.setAddrProvince(String.valueOf(params.get("addrProvince")));
		ua.setAddrCity(String.valueOf(params.get("addrCity")));
		ua.setAddrArea(String.valueOf(params.get("addrArea")));
		ua.setAddrAddress(String.valueOf(params.get("addrAddress")));
		ua.setAddrReceiver(String.valueOf(params.get("addrReceiver")));
		ua.setAddrTel(String.valueOf(params.get("addrTel")));
		ua.setAddrZipCode(String.valueOf(params.get("addrZipCode")));
		Map<String,Object> m = new HashMap<>();
		m.put("userId",String.valueOf(params.get("userId")));
		int num = userAddressService.count(m);
		if(num == 0){
			ua.setAddrIsDefault(1);
		}
		ua.setAddrId(idWorker.nextId());
		ua.setAddrCreateTime(new Date());
		//UserInfoDO userInfo = (UserInfoDO) request.getSession().getAttribute("userInfo");
		//UserInfoDO userInfo = LoginUtils.getCurrentUser(request);
		ua.setUserId(String.valueOf(params.get("userId")));
		int n = userAddressService.save(ua);
		if(n > 0){
			return ResultUtil.renderSuccess();
		}else{
			return ResultUtil.renderError();
		}
	}


	/*
	* 修改收货地址
	* */
	@RequestMapping("/wx/userAddress/updateAddress")
	@ResponseBody
	public JsonResult updateAddress(UserAddressDO ua,HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		ua.setAddrUpdateTime(new Date());
		int num = userAddressService.update(ua);
		if(num > 0){
			return ResultUtil.renderSuccess();
		}else{
			return ResultUtil.renderError();
		}
	}


	/*
	* 设置默认收货地址，会把之前设置的记录清理掉
	* */
	@RequestMapping("/wx/userAddress/setDefualtAddress")
	@ResponseBody
	public JsonResult setDefualtAddress(String addressId,String userId,HttpServletRequest request,HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		//UserInfoDO userInfo = LoginUtils.getCurrentUser(request);
		int num = userAddressService.setDefualtAddress(userId,addressId);
		if(num > 0 ){
			return ResultUtil.renderSuccess();
		}else{
			return ResultUtil.renderError();
		}
	}

	/*
	* 获取收货地址列表
	* */
	@RequestMapping("/wx/userAddress/listAddress")
	@ResponseBody
	public JsonResult listAddress(String userId,HttpServletResponse response,HttpServletRequest request){
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String,Object> map = new HashMap<>();
		map.put("userId",userId);
		List<UserAddressDO> list = userAddressService.list(map);
		return ResultUtil.renderSuccess(list);
	}

	/*
    * 删除收货地址
    */
	@RequestMapping("/wx/userAddress/deleteAddress")
	@ResponseBody
	public JsonResult deleteAddress(String addressId, HttpServletRequest request, HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		int num = userAddressService.remove(Long.parseLong(addressId));
		if(num > 0){
			return ResultUtil.renderSuccess();
		}else{
			return ResultUtil.renderError();
		}
	}


	/*
    * 获取默认收货地址
    */
	@RequestMapping("/wx/userAddress/getDefualtAddr")
	@ResponseBody
	public JsonResult getDefualtAddr(String userId,HttpServletRequest request, HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String,Object> map = new HashMap<>();
		if(userId == null){
			return ResultUtil.renderError();
		}
		map.put("userId",userId);
		map.put("addrIsDefault",1);
		List<UserAddressDO> list = userAddressService.list(map);
		return ResultUtil.renderSuccess(list);
	}
}
